<?php

/**
 * @file classes/manager/form/CTTMTGForm.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class CTTMTGForm
 * @ingroup manager_form
 *
 * @brief Form for creating and modifying journal CTTMTGs.
 */

// $Id$


import('lib.pkp.classes.form.Form');

class CTTMTGForm extends Form {

	/** @var $CTTMTGId int The ID of the CTTMTG being edited */
	var $CTTMTGId;

	/** @var $includeCTTMTGEditor object Additional CTTMTG editor to
	 *       include in assigned list for this CTTMTG
	 */
	var $includeCTTMTGEditor;

	/** @var $omitCTTMTGEditor object Assigned CTTMTG editor to omit from
	 *       assigned list for this CTTMTG
	 */
	var $omitCTTMTGEditor;

	/** @var $CTTMTGEditors array List of user objects representing the
	 *       available CTTMTG editors for this journal.
	 */
	var $CTTMTGEditors;

	/**
	 * Constructor.
	 * @param $journalId int omit for a new journal
	 */
	function CTTMTGForm($CTTMTGId = null) {

		parent::Form('acctCN/CTTMTGForm.tpl');

		$journal =& Request::getJournal();
		$this->CTTMTGId = $CTTMTGId;

		// Validation checks for this form
		$this->addCheck(new FormValidatorLocale($this, 'matk', 'required', 'Phai co Ma TK!'));
		$this->addCheck(new FormValidatorLocale($this, 'tentk', 'required', 'Phai co Ten TK'));
		$this->addCheck(new FormValidatorPost($this));
		

		// Get a list of CTTMTG editors for this journal.
		$roleDao =& DAORegistry::getDAO('RoleDAO');
		$this->CTTMTGEditors =& $roleDao->getUsersByRoleId(ROLE_ID_TMTG_EDITOR, $journal->getId());
		$this->CTTMTGEditors =& $this->CTTMTGEditors->toArray();

		
	}

	/**
	 * When displaying the form, include the specified CTTMTG editor
	 * in the assigned list for this CTTMTG.
	 * @param $CTTMTGEditorId int
	 */
	function includeCTTMTGEditor($CTTMTGEditorId) {
		foreach ($this->CTTMTGEditors as $key => $junk) {
			if ($this->CTTMTGEditors[$key]->getId() == $CTTMTGEditorId) {
				$this->includeCTTMTGEditor =& $this->CTTMTGEditors[$key];
			}
		}
	}

	/**
	 * When displaying the form, omit the specified CTTMTG editor from
	 * the assigned list for this CTTMTG.
	 */
	function omitCTTMTGEditor($CTTMTGEditorId) {
		foreach ($this->CTTMTGEditors as $key => $junk) {
			if ($this->CTTMTGEditors[$key]->getId() == $CTTMTGEditorId) {
				$this->omitCTTMTGEditor =& $this->CTTMTGEditors[$key];
			}
		}
	}

	/**
	 * Get the names of fields for which localized data is allowed.
	 * @return array
	 */
	function getLocaleFieldNames() {
		$CTTMTGDao =& DAORegistry::getDAO('CTTMTGDAO');
		return $CTTMTGDao->getLocaleFieldNames();
	}

	/**
	 * Display the form.
	 */
	function display() {
		$journal =& Request::getJournal();
		
		parent::display();
	}

	/**
	 * Initialize form data from current settings.
	 */
	function initData() {
		$journal =& Request::getJournal();
		if (isset($this->CTTMTGId)) {
			$CTTMTGDao =& DAORegistry::getDAO('CTTMTGDAO');
			$CTTMTG =& $CTTMTGDao->getCTTMTG($this->CTTMTGId, false, false);
			if ($CTTMTG == null) {
				unset($this->CTTMTGId);
			} else {
				$this->_data = array(
					'matk' => $CTTMTG->getmatk(), // Localized
					'tentk' => $CTTMTG->gettentk(), // Localized
				);
			}
		}
	}

	/**
	 * Assign form data to user-submitted data.
	 */
	function readInputData() {
		$this->readUserVars(array('matk', 'tentk'));
	}

	/**
	 * Save CTTMTG.
	 */
	function execute() {

		$CTTMTGDao =& DAORegistry::getDAO('CTTMTGDAO');

		if (isset($this->CTTMTGId)) {
			$CTTMTG =& $CTTMTGDao->getCTTMTG($this->CTTMTGId, false, false);
		}

		if (!isset($CTTMTG)) {
			$CTTMTG = new CTTMTG();
		}

		$CTTMTG->setmatk($this->getData('matk'), null); // Localized
		$CTTMTG->settentk($this->getData('tentk'), null); // Localized

		
		if (isset($this->CTTMTGId)) {
			$CTTMTGDao->updateCTTMTG($CTTMTG);
			$CTTMTGId = $CTTMTG->getId();

		} else {

			$CTTMTGId = $CTTMTGDao->insertCTTMTG($CTTMTG);
			
		}

	}
}

?>
